
#include "cybertron/core/UtilConsole.hpp"
#include "cybertron/core/UtilString.hpp"
#include "../PlatformHeader.hpp"

CYBERTRON_BEGIN

CybertronCoreAPI void UtilConsole::setTitle(const std::string& title)
{
#ifdef CYBERTRON_WIN
	std::wstring titleW = UtilString::u2w(title);
	SetConsoleTitleW(titleW.c_str());
#endif
}

CybertronCoreAPI void UtilConsole::setColor(
	EConsoleColor foregroundColor,
	EConsoleColor backgroundColor)
{
#ifdef CYBERTRON_WIN
	HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
	WORD attr = 0;
	switch (foregroundColor)
	{
	case EConsoleColor_Blue:
		attr |= (FOREGROUND_BLUE | FOREGROUND_INTENSITY);
		break;
	case EConsoleColor_DarkBlue:
		attr |= (FOREGROUND_BLUE);
		break;
	case EConsoleColor_Cyan:
		attr |= (FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY);
		break;
	case EConsoleColor_DarkCyan:
		attr |= (FOREGROUND_GREEN | FOREGROUND_BLUE);
		break;
	case EConsoleColor_Gray:
		attr |= (FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
		break;
	case EConsoleColor_DarkGray:
		attr |= (FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
		break;
	case EConsoleColor_Green:
		attr |= (FOREGROUND_GREEN | FOREGROUND_INTENSITY);
		break;
	case EConsoleColor_DarkGreen:
		attr |= (FOREGROUND_GREEN);
		break;
	case EConsoleColor_Magenta:
		attr |= (FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY);
		break;
	case EConsoleColor_DarkMagenta:
		attr |= (FOREGROUND_RED | FOREGROUND_BLUE);
		break;
	case EConsoleColor_Red:
		attr |= (FOREGROUND_RED | FOREGROUND_INTENSITY);
		break;
	case EConsoleColor_DarkRed:
		attr |= (FOREGROUND_RED);
		break;
	case EConsoleColor_Yellow:
		attr |= (FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY);
		break;
	case EConsoleColor_DarkYellow:
		attr |= (FOREGROUND_RED | FOREGROUND_GREEN);
		break;
	case EConsoleColor_White:
		attr |= (FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY);
		break;
	case EConsoleColor_Black:
	default:
		break;
	}

	switch (backgroundColor)
	{
	case EConsoleColor_Blue:
		attr |= (BACKGROUND_BLUE | BACKGROUND_INTENSITY);
		break;
	case EConsoleColor_DarkBlue:
		attr |= (BACKGROUND_BLUE);
		break;
	case EConsoleColor_Cyan:
		attr |= (BACKGROUND_GREEN | BACKGROUND_BLUE | BACKGROUND_INTENSITY);
		break;
	case EConsoleColor_DarkCyan:
		attr |= (BACKGROUND_GREEN | BACKGROUND_BLUE);
		break;
	case EConsoleColor_Gray:
		attr |= (BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE);
		break;
	case EConsoleColor_DarkGray:
		attr |= (BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE);
		break;
	case EConsoleColor_Green:
		attr |= (BACKGROUND_GREEN | BACKGROUND_INTENSITY);
		break;
	case EConsoleColor_DarkGreen:
		attr |= (BACKGROUND_GREEN);
		break;
	case EConsoleColor_Magenta:
		attr |= (BACKGROUND_RED | BACKGROUND_BLUE | BACKGROUND_INTENSITY);
		break;
	case EConsoleColor_DarkMagenta:
		attr |= (BACKGROUND_RED | BACKGROUND_BLUE);
		break;
	case EConsoleColor_Red:
		attr |= (BACKGROUND_RED | BACKGROUND_INTENSITY);
		break;
	case EConsoleColor_DarkRed:
		attr |= (BACKGROUND_RED);
		break;
	case EConsoleColor_Yellow:
		attr |= (BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_INTENSITY);
		break;
	case EConsoleColor_DarkYellow:
		attr |= (BACKGROUND_RED | BACKGROUND_GREEN);
		break;
	case EConsoleColor_White:
		attr |= (BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE | BACKGROUND_INTENSITY);
		break;
	case EConsoleColor_Black:
	default:
		break;
	}
	SetConsoleTextAttribute(hConsole, attr);

	//GetConsole
#endif
}
CybertronCoreAPI bool UtilConsole::getColor(unsigned short& attr) {
#ifdef CYBERTRON_WIN
	CONSOLE_SCREEN_BUFFER_INFOEX info;
	info.cbSize = 96;
	HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
	if (!GetConsoleScreenBufferInfoEx(hConsole, &info)) {
		return false;
	}
	attr = info.wAttributes;
#endif
	return true;
}

CybertronCoreAPI  bool UtilConsole::setColor(unsigned short attr) {
#ifdef CYBERTRON_WIN
	HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleTextAttribute(hConsole, attr);
#endif
	return true;
}
CYBERTRON_END
